package cn.yeyiu.netonlinevideo.parser.video;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.parser.Parser;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoInfo;

public class KelaAnimeParser extends Parser {
    public KelaAnimeParser(SourceData source) {
        super(source);
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        String url = "https://www.kelatv.com/index.php?s=home-vod-type-id-3-mcid--area-大陆-letter--order--picm-1-p-%d";
        url = String.format(url, pageIndex + 1);
        try{
            return Jsoup.connect(url).get();
        }catch (IOException e){
            AndroidUtil.toast("访问失败！");
        }
        return null;
    }

    @Override
    public List<MediaData> getDataList(Object data, String type) {
        Document document = (Document) data;
        Element ul = document.selectXpath("//ul[id='content']").first();

        List<MediaData> videoInfos = new ArrayList<>();
        MediaData mediaData;
        VideoInfo videoInfo;
        for (Element li : ul.getElementsByTag("li")) {
            videoInfo = new VideoInfo();
            mediaData = new MediaData();

            videoInfo.code = source.code;
            videoInfo.type = type;
            videoInfo.title = li.selectXpath("./li/div/h5/a").get(0).text();
            videoInfo.picSrc = li.selectXpath("./li/a/img").get(0).attr("src");
            videoInfo.videoSrc = videoInfo.picSrc.replace("cover.jpg", "720/play.m3u8");
            videoInfo.info = type;

            mediaData.wrap(videoInfo);
            videoInfos.add(mediaData);
        }
        return videoInfos;
    }

}
